package service

import (
	"fmt"
	"gorm.io/gorm"
	"zk2/internal/dao"
	"zk2/internal/dto"
	"zk2/internal/models"
)

type FriendServiceImpl interface {
	FriendAdd(req dto.FriendReq) (resp *dto.FriendResp, err error)
}
type FriendService struct {
}

func NewFriendService() *FriendService {
	return &FriendService{}
}
func (f *FriendService) FriendAdd(req dto.FriendReq) (resp *dto.FriendResp, err error) {
	friendDao := dao.NewFriendDao()
	apply := &models.FriendApply{
		Model:  gorm.Model{},
		UserId: req.UserId,
		DistId: req.DistId,
		Status: req.Status,
	}
	add, err := friendDao.FriendAdd(apply)
	if err != nil {
		return
	}
	if req.Status == 2 {
		friend := &models.Friend{
			Model:  gorm.Model{},
			UserId: req.UserId,
			DistId: req.DistId,
		}
		creat, err := friendDao.FriendCreat(friend)
		if err != nil {
			return nil, err
		}
		fmt.Println(creat)
	}
	return &dto.FriendResp{
		Friend: add,
	}, nil
}
